#include "CMainMenuState.h"
#include "CGame.h"
#include "COptionMenuState.h"
#include "CGamePlayState.h"
#include "CXInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectSound.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CConfigAudioState.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "CConfigControlState.h"
#include "CConfigVideoState.h"
#include "CExitState.h"
#include "Mail System.h"
#include "CUnit.h"
#include "CObjectManager.h"
#include "CObjGenerator.h"
#include "CEventSystem.h"

CConfigAudioState::CConfigAudioState()
{
	Type(SUB);
	m_bfItem.Init("Ludicrum_Reclamation_Alt.fnt");
	m_nBGImage = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/piedpiper2.jpg");
	MusicInput = false;
	SFXInput = false;

}
CConfigAudioState::~CConfigAudioState()
{

}
void CConfigAudioState::Enter()
{
	/////////////////////////////////////////////////
	//	Menu Items
	CMenuItem miPlay;
	miPlay.Init(&m_bfItem, 200, 0, "Music_Volume", CConfigVideoState::GetInstance(), 1.0f);
	miPlay.GoTo(200, 300);
	m_vMenuItems.push_back(miPlay);
	CMenuItem miOptions;
	miOptions.Init(&m_bfItem, 200, 0, "SFX_Volume", CConfigAudioState::GetInstance(), 1.0f);
	miOptions.GoTo(400, 300);
	m_vMenuItems.push_back(miOptions);
	/*CMenuItem miKer;
	miKer.Init(&m_bfItem, 200, 0, "Controls", CConfigControlState::GetInstance(), 1.0f);
	miKer.GoTo(600, 300);
	m_vMenuItems.push_back(miKer);*/
	CMenuItem miExit;
	miExit.Init(&m_bfItem, 200, 0, "Back", CExitState::GetInstance(), 1.0f);
	miExit.GoTo(600, 300);
	m_vMenuItems.push_back(miExit);
	//
	/////////////////////////////////////////////////

	//ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CUnit>("CUnit");
	//MailSystem::GetInstance()->InitMessageSystem(CGame::GetInstance()->MessageProc);

	//for(int i = 0; i < 100; ++i)
	//	MailSystem::GetInstance()->SendMail(new SpawnUnitMenu());


	//MailSystem::GetInstance()->ReadMail();
}
void CConfigAudioState::Exit()
{
	m_vMenuItems.clear();

	/*MailSystem::GetInstance()->ShutdownMailSystem();
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->ShutdownObjectFactory();*/
	//CObjectManager::GetInstance()->Clear();
}
bool CConfigAudioState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->PopState();
	}

	if(MusicInput)
	{
		if((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RIGHT) ||
			(CXInput::GetInstance()->LStickX() >= 7850) && CXInput::GetInstance()->IsLStickMoved() == false))
		{
			CXInput::GetInstance()->IsLStickMoved(true);
			if(CGame::GetInstance()->MusicVol() < 1.0)
				CGame::GetInstance()->MusicVol(CGame::GetInstance()->MusicVol() + 0.1f);
			CSGD_FModManager::GetInstance()->SetVolume(CMainMenuState::GetInstance()->BGMusic(), CGame::GetInstance()->MusicVol());

		}
		if((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_LEFT) ||
			(CXInput::GetInstance()->LStickX() <= -7850) && CXInput::GetInstance()->IsLStickMoved() == false))
		{
			CXInput::GetInstance()->IsLStickMoved(true);
			if(CGame::GetInstance()->MusicVol() > 0.0)
				CGame::GetInstance()->MusicVol(CGame::GetInstance()->MusicVol() - 0.1f);
			CSGD_FModManager::GetInstance()->SetVolume(CMainMenuState::GetInstance()->BGMusic(), CGame::GetInstance()->MusicVol());

		}
		if(	CXInput::GetInstance()->LStickX() < 7850 &&	CXInput::GetInstance()->LStickX() > -7850
			&& CXInput::GetInstance()->IsLStickMoved() == true)
		{
			CXInput::GetInstance()->IsLStickMoved(false);
		}
		if(	((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN)) && (CSGD_DirectInput::GetInstance()->KeyUp(DIK_LALT))) ||
			CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A && CXInput::GetInstance()->GetIsADown() == false)
		{
			CXInput::GetInstance()->SetIsADown(true);
			MusicInput = false;
		}
		else if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A) && CXInput::GetInstance()->GetIsADown() == true)
			CXInput::GetInstance()->SetIsADown(false);

		return true;
	}
	else if(SFXInput)
	{
		if((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RIGHT) ||
			(CXInput::GetInstance()->LStickX() >= 7850) && CXInput::GetInstance()->IsLStickMoved() == false))
		{
			CXInput::GetInstance()->IsLStickMoved(true);
			if(CGame::GetInstance()->SFXVol() < 1.0)
				CGame::GetInstance()->SFXVol(CGame::GetInstance()->SFXVol() + 0.1f);
			CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
			CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
			
		}
		if((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_LEFT) ||
			(CXInput::GetInstance()->LStickX() <= -7850) && CXInput::GetInstance()->IsLStickMoved() == false))
		{
			CXInput::GetInstance()->IsLStickMoved(true);
			if(CGame::GetInstance()->SFXVol() > 0.0)
				CGame::GetInstance()->SFXVol(CGame::GetInstance()->SFXVol() - 0.1f);
			CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
			CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
			
		}
		if(	CXInput::GetInstance()->LStickX() < 7850 &&	CXInput::GetInstance()->LStickX() > -7850
			&& CXInput::GetInstance()->IsLStickMoved() == true)
		{
			CXInput::GetInstance()->IsLStickMoved(false);
		}
		if(	((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN)) && (CSGD_DirectInput::GetInstance()->KeyUp(DIK_LALT))) ||
			CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A && CXInput::GetInstance()->GetIsADown() == false)
		{
			CXInput::GetInstance()->SetIsADown(true);
			SFXInput = false;
		}
		else if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A) && CXInput::GetInstance()->GetIsADown() == true)
			CXInput::GetInstance()->SetIsADown(false);

		return true;

	}
	else
	{
		if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_DOWN) || 
			CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RIGHT) ||
			((CXInput::GetInstance()->LStickY() <= -7850 ||
			CXInput::GetInstance()->LStickX() >= 7850) && CXInput::GetInstance()->IsLStickMoved() == false))
		{

			//////////////////////////////////////////////////////////
			//	BUG FIX
			//	Reference Bug # BB-006
			//	Approved by : ?
			//	BUG FIX START
			//////////////////////////////////////////////////////////
			CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChosenSound());
			CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChosenSound(), CGame::GetInstance()->SFXVol());
			CXInput::GetInstance()->IsLStickMoved(true);
			vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
			for(unsigned int i = 0; i < m_vMenuItems.size() - 1; i++)
				StuffIter++;
			float fTempX = StuffIter->TarPosX();
			float fTempY = StuffIter->TarPosY();

			while(StuffIter != m_vMenuItems.begin())
			{
				vector<CMenuItem>::iterator temp = StuffIter;

				--StuffIter;

				temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

				if(StuffIter->Target() == m_vMenuItems[0].Target())
				{
					StuffIter->GoTo(fTempX, fTempY);
					break;
				}
			}
			//////////////////////////////////////////////////////////
			//	BUG FIX END Reference # BB-006
			//////////////////////////////////////////////////////////
		}

		if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_UP) || 
			CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_LEFT) ||
			((CXInput::GetInstance()->LStickY() >= 7850 ||
			CXInput::GetInstance()->LStickX() <= -7850) && CXInput::GetInstance()->IsLStickMoved() == false))
		{
			CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
			CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
			CXInput::GetInstance()->IsLStickMoved(true);
			vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
			float fTempX = StuffIter->TarPosX();
			float fTempY = StuffIter->TarPosY();

			while(StuffIter != m_vMenuItems.end())
			{
				vector<CMenuItem>::iterator temp = StuffIter;

				StuffIter++;

				temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

				if(StuffIter->Name() == m_vMenuItems[m_vMenuItems.size()-1].Name())
				{
					StuffIter->GoTo(fTempX, fTempY);
					break;
				}
			}
		}
		if((CXInput::GetInstance()->LStickY() > -7850 && CXInput::GetInstance()->LStickY() < 7850 &&
			CXInput::GetInstance()->LStickX() < 7850 &&	CXInput::GetInstance()->LStickX() > -7850)
			&& CXInput::GetInstance()->IsLStickMoved() == true)
		{
			CXInput::GetInstance()->IsLStickMoved(false);
		}

		if(	((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN)) && (CSGD_DirectInput::GetInstance()->KeyUp(DIK_LALT))) ||
			CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A && CXInput::GetInstance()->GetIsADown() == false)
		{
			CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChosenSound());
			CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChosenSound(), CGame::GetInstance()->SFXVol());
			CXInput::GetInstance()->SetIsADown(true);
			vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

			while(StuffIter != m_vMenuItems.end())
			{
				if(StuffIter->TarPosX() == 200)
				{
					if(StuffIter->Name() == "Back")
					{
						CGame::GetInstance()->PopState();
						return true;
					}
					if(StuffIter->Name() == "Music_Volume")
					{
						MusicInput = true;
						return true;
					}
					if(StuffIter->Name() == "SFX_Volume")
					{
						SFXInput = true;
						return true;
					}
				}
				++StuffIter;
			}
		}
		else if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A) && CXInput::GetInstance()->GetIsADown() == true)
			CXInput::GetInstance()->SetIsADown(false);
		return true;
	}
}

void CConfigAudioState::Update(float fDelta)
{
	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		StuffIter->Update(fDelta);

		++StuffIter;
	}


	/*MailSystem::GetInstance()->ReadMail();
	CEventSystem::GetInstance()->ProcessEvents();
	CObjectManager::GetInstance()->Update(fDelta);*/
}
void CConfigAudioState::Render()
{
	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-005
	// Approved by: 
	// BUG FIX START
	/////////////////////////////////////////////////////////////////
	CSGD_TextureManager::GetInstance()->Draw(m_nBGImage,0, 0, .8f, .6f);
	///////////////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-005
	//////////////////////////////////////////////////////////////////////////

	//CObjectManager::GetInstance()->Render();

	m_bfItem.DrawBitmapText("Audio_Config_Menu", 25, 100, 2.0f);

	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		if(StuffIter->TarPosX() == 200)
			StuffIter->Render(true);
		else
			StuffIter->Render();

		if(StuffIter->TarPosX() == 200)
		{
			CSGD_Direct3D::GetInstance()->SpriteEnd();
			char buffer[128];
			if(StuffIter->Name() == "Music_Volume")
			{
				sprintf_s(buffer, _countof(buffer), "%f", CGame::GetInstance()->MusicVol());
				CSGD_Direct3D::GetInstance()->DrawText(buffer, (int)StuffIter->PosX(), (int)StuffIter->PosY() + 50);
			}
			if(StuffIter->Name() == "SFX_Volume")
			{
				sprintf_s(buffer, _countof(buffer), "%f", CGame::GetInstance()->SFXVol());
				CSGD_Direct3D::GetInstance()->DrawText(buffer, (int)StuffIter->PosX(), (int)StuffIter->PosY() + 50);
			}
			CSGD_Direct3D::GetInstance()->SpriteBegin();
		}

		++StuffIter;
	}
}